﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using DbSharp.Framework.DbObject;
using DbSharp.Framework.Common;

namespace DbSharp.Framework.CaseFile.Parser
{
    class DataPumpSectionParser
    {
        private DataPumpSection m_PumpSection = new DataPumpSection();

        public DataPumpSectionParser(string caseName, IEnumerable<IStatementDeclaration> listStatement, XElement globalSettingElement, XElement pumpSectionRootElement)
        {
            var srcStatementRootElement = pumpSectionRootElement.Element("pumpSourceStatement");
            var srcParser = new DataPumpStatementParser(caseName, listStatement, globalSettingElement, srcStatementRootElement);
            m_PumpSection.PumpSrcResultsetSection = srcParser.StatementSection;

            var destStatementRootElement = pumpSectionRootElement.Element("pumpDestinationStatement");
            var destParser = new DataPumpStatementParser(caseName, listStatement, globalSettingElement, destStatementRootElement);
            m_PumpSection.PumpDestResultsetSection = destParser.StatementSection;
        }

        public DataPumpSection PumpSection
        {
            get { return m_PumpSection; }
        }
    }

    class DataPumpStatementParser
    {
        private ResultsetSection m_StatementSection = new ResultsetSection();

        public DataPumpStatementParser(string caseName, IEnumerable<IStatementDeclaration> listStatement, XElement globalSettingElement, XElement statementRootElement)
        { 
            var defaultOutputSettingParser = new DefaultRsOutputSettingParser(globalSettingElement);
            var defaultRsOutputSetting = defaultOutputSettingParser.Setting;

            var aliasAttribute = statementRootElement.Attribute("statementAlias");
            string statementAlias = aliasAttribute.Value.Trim();
            IStatementDeclaration statementDeclaration = listStatement.FirstOrDefault(
                sql => StatementDeclarationExt.FindByAlias(sql, statementAlias));

            if (statementDeclaration == null)
            {
                string messageFmt = "statementAlias'{0}' used in Test Case '{1}' cannot be found in sqlDeclarations and procDeclarations";
                string message = string.Format(messageFmt, statementAlias, caseName);
                throw new DbSharpException(message);
            }

            //extract inputArguments for this ProcedureDeclaration
            XElement procInputArgsElement;
            procInputArgsElement = statementRootElement.Element("inputArguments");
            var inputArgsProcessor = new StatementInputArgsProcessor(procInputArgsElement, statementDeclaration);
            var statementObj = inputArgsProcessor.StatementWithInArgValues;

            //Init ResultsetSection object
            var setting = ResultsetOutputSettingHelper.CreateResultsetOutputSetting(statementRootElement, defaultRsOutputSetting);
            m_StatementSection.OutputSetting = setting;
            m_StatementSection.StatementDeclaration = statementObj;
            m_StatementSection.IsTargetResultset = false;
        }

         

        public ResultsetSection StatementSection
        {
            get { return m_StatementSection; }
        }


      }
 
}
